package com.lowca.news.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "t_role", catalog = "com_lowca_news")
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends IdEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	private String name;// 角色名
	private String description;// 描述
	private Set<User> users = new HashSet<User>(0);

	public Role() {
		super();
	}

	public Role(String name, String description, Set<User> users) {
		super();
		this.name = name;
		this.description = description;
		this.users = users;
	}

	@Column(name = "name", length = 80, nullable = false, unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "description", length = 255)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "roles")
	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	public boolean equals(Object other) {
		if (this == other)
			return true;
		if (!(other instanceof Role))
			return false;
		final Role role = (Role) other;
		if (!role.getName().equals(getName()))
			return false;
		return true;
	}

	public int hashCode() {
		int result;
		result = getName().hashCode();
		return result;
	}

}
